package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.FdsCleaningRulesetMapper;
import com.yunhai.device.service.FdsCleaningRulesetService;
import com.yunhai.model.entity.fds.FdsCleaningRulesetEntity;
import com.yunhai.model.vo.fds.FdsCleaningRulesetQuery;
import com.yunhai.model.vo.fds.FdsCleaningRulesetVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FdsCleaningRulesetServiceImpl implements FdsCleaningRulesetService {

    @Resource
    private FdsCleaningRulesetMapper fdsCleaningRulesetMapper;

    /**
     * 获取 FdsCleaningRuleset 分页列表
     */
    @Transactional
    public IPage getFdsCleaningRulesetPagination(PaginationVo paginationVo, FdsCleaningRulesetQuery fdsCleaningRulesetQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FdsCleaningRulesetEntity> lqs = new LambdaQueryWrapper();
        fdsCleaningRulesetMapper.selectPage(page, lqs);

        List<FdsCleaningRulesetEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FdsCleaningRuleset 所有数据
     *
     */
    @Transactional
    public List<FdsCleaningRulesetEntity> getFdsCleaningRulesetAllList( FdsCleaningRulesetQuery fdsCleaningRulesetQuery ) throws Exception {

        LambdaQueryWrapper<FdsCleaningRulesetEntity> lqs = new LambdaQueryWrapper();
        List<FdsCleaningRulesetEntity> list = fdsCleaningRulesetMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FdsCleaningRuleset 详情
     */
    @Transactional
    public FdsCleaningRulesetEntity getFdsCleaningRulesetDetail( Long id) throws Exception {

        FdsCleaningRulesetEntity fdsCleaningRulesetEntity = fdsCleaningRulesetMapper.selectById(id);
        return fdsCleaningRulesetEntity;
    }


    /**
     * 新增 FdsCleaningRuleset
     * */
    @Transactional
    public void addFdsCleaningRuleset(FdsCleaningRulesetVo fdsCleaningRulesetVo) throws Exception {

        FdsCleaningRulesetEntity fdsCleaningRulesetEntity = new FdsCleaningRulesetEntity();
        ReflectionCopy.copyProperties( fdsCleaningRulesetEntity, fdsCleaningRulesetVo );
        fdsCleaningRulesetMapper.insert(fdsCleaningRulesetEntity);
        return;
    }


    /**
     * 更新 FdsCleaningRuleset
     * */
    @Transactional
    public void updateFdsCleaningRuleset( FdsCleaningRulesetVo fdsCleaningRulesetVo) throws Exception {

        if( fdsCleaningRulesetVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FdsCleaningRulesetEntity fdsCleaningRulesetEntity = new FdsCleaningRulesetEntity();
        ReflectionCopy.copyProperties( fdsCleaningRulesetEntity, fdsCleaningRulesetVo );
        fdsCleaningRulesetMapper.updateById(fdsCleaningRulesetEntity);
        return;
    }

    /**
     * 删除 FdsCleaningRuleset
     * */
    @Transactional
    public void batchRemoveFdsCleaningRuleset( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fdsCleaningRulesetMapper.deleteBatchIds(ids);
    }

}